#!/usr/bin/env python3
"""


"""

import sys
import argparse
import logging
from pathlib import Path

def setup_logging(level='INFO'):
    """"""
    logging.basicConfig(
        level=getattr(logging, level),
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(),
            logging.FileHandler('video_summary.log', encoding='utf-8')
        ]
    )

def check_dependencies():
    """"""
    missing_deps = []
    
    try:
        import whisper
    except ImportError:
        missing_deps.append('openai-whisper')
    
    try:
        import streamlit
    except ImportError:
        missing_deps.append('streamlit')
    
    try:
        import requests
    except ImportError:
        missing_deps.append('requests')
    
    if missing_deps:
        print(f" : {', '.join(missing_deps)}")
        print(": pip install -r requirements.txt")
        return False
    
    return True

def run_web_app():
    """Web"""
    print(" Web...")
    try:
        from src.web.app import main
        main()
    except Exception as e:
        print(f" Web: {e}")
        return False
    return True

def run_desktop_app():
    """"""
    print(" ...")
    try:
        from src.desktop.app import main
        main()
    except Exception as e:
        print(f" : {e}")
        return False
    return True

def run_mobile_app():
    """"""
    print(" ...")
    try:
        from src.mobile.app import main
        main()
    except Exception as e:
        print(f" : {e}")
        return False
    return True

def run_cli():
    """"""
    print(" ...")
    try:
        from src.core.cli import main
        main()
    except Exception as e:
        print(f" : {e}")
        return False
    return True

def build_mobile_app():
    """"""
    print(" ...")
    import subprocess
    import os
    
    if not os.path.exists('buildozer.spec'):
        print(" buildozer.spec")
        return False
    
    try:
        # Android APK
        result = subprocess.run(['buildozer', 'android', 'debug'], 
                              capture_output=True, text=True)
        
        if result.returncode == 0:
            print(" !")
            print("APK: bin/")
        else:
            print(f" : {result.stderr}")
            return False
            
    except FileNotFoundError:
        print(" buildozer: pip install buildozer")
        return False
    
    return True

def main():
    """"""
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('--mode', '-m', 
                       choices=['web', 'desktop', 'mobile', 'cli', 'build-mobile'],
                       default='web',
                       help=' (: web)')
    parser.add_argument('--log-level', '-l',
                       choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
                       default='INFO',
                       help=' (: INFO)')
    parser.add_argument('--check-deps', '-c',
                       action='store_true',
                       help='')
    
    args = parser.parse_args()
    
    # 
    setup_logging(args.log_level)
    
    # 
    if args.check_deps or not check_dependencies():
        return
    
    print(" ")
    print("=" * 50)
    
    # 
    if args.mode == 'web':
        run_web_app()
    elif args.mode == 'desktop':
        run_desktop_app()
    elif args.mode == 'mobile':
        run_mobile_app()
    elif args.mode == 'cli':
        run_cli()
    elif args.mode == 'build-mobile':
        build_mobile_app()
    else:
        print(f" : {args.mode}")

if __name__ == "__main__":
    main()